package de.ars.client.items;

import java.util.Enumeration;
import java.util.Vector;
import javax.microedition.lcdui.Graphics;

import de.ars.client.animator.IAnimationEventListener;
import de.ars.client.animator.Point;
import de.ars.client.datahandling.PropertyReader;
import de.ars.client.items.animators.OptionItemAnimator;
import de.ars.client.items.elements.ListItem;
import de.ars.client.paintSystems.ClassicLayout;
import de.ars.client.paintSystems.StringPainter;
import de.ars.client.screens.MainScreen;

public class OptionItem extends AbstractSlideItem
{
	private int optionStatePressed;
	private static final int STATE_NOTHING = 0;
	private static final int STATE_OPEN_EDIT_ANIMATION = 1;
	private static final int STATE_OPEN_ADD_PERSON_ANIMATION = 2;
	private static final int STATE_OPEN_ADD_OPTION_ANIMATION = 3; 
	private static final int STATE_SEND_SMS_ANIMATION = 4;
	private static final int STATE_CALL_PERSON_ANIMATION = 5;
	private static final int STATE_IMPORT_PICTURE_ANIMATION = 6;
	private static final int STATE_SYNC_WITH_CSV_ANIMATION = 7;
	private static final int DETAIL_OPTION = 1;
	private static final int NORMAL_OPTION = 2;
	private static final int BOTH_OPTION = 3;

	private int key;

	public OptionItem(int _width, int _height, MainScreen _parent, StringPainter _stringPainter)
	{
		super(_width,_height,_parent,_stringPainter);
		
		this.optionStatePressed = STATE_NOTHING;
		
		this.layout = new ClassicLayout( this.screenWidth, this.screenHeight );
		
		this.animator = new OptionItemAnimator(this);
        this.animator.createNewAnimationPoint(this.layout.getOptionClosePos());
        this.animator.createNewAnimationPoint(this.layout.getOptionCloseSize());
        this.animator.createNewAnimationPoint(this.layout.getOptionCloseTextPos());
        this.animator.setLayout(this.layout);
        
        this.entryItems = new Vector();        
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.delete"), BOTH_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.edit"), DETAIL_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.addoption"), DETAIL_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.addperson"), NORMAL_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.callperson"), BOTH_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.sendsms"), BOTH_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.sync"), BOTH_OPTION));
        this.entryItems.addElement(new ListItem(this.stringPainter, PropertyReader.getProperty("optionitem.entry.picture"), BOTH_OPTION));
        
	}
	
	private void calcPlusIndex(int showOption)
	{
		int count = 0;
		boolean foundEnd = false;
		for(int i = this.selectedItemIndex + 1; i < this.entryItems.size() && !foundEnd; i++)
		{
			if(((ListItem)this.entryItems.elementAt(i)).getShowOption() == showOption)
			{
				count++;
			}
			else
			{
				foundEnd = true;
			}
		}
		this.selectedItemIndex += count + 1;
	}
	
	private void calcMinusIndex(int showOption)
	{
		int count = 0;
		boolean foundEnd = false;
		for(int i = this.selectedItemIndex - 1; i > 0 && !foundEnd; i--)
		{
			if(((ListItem)this.entryItems.elementAt(i)).getShowOption() == showOption)
			{
				count++;
			}
			else
			{
				foundEnd = true;
			}
		}
		this.selectedItemIndex -= count + 1;
	}
	
	public boolean keyPressed(int _key)
	{
		if (isOpen())
		{
			this.key = _key;
			switch(this.key)
			{
				case -1:
					if(this.selectedItemIndex > 0)
					{
						if(!this.parent.getItem().getDetailStatus())
							this.calcMinusIndex(DETAIL_OPTION);
						else
							this.calcMinusIndex(NORMAL_OPTION);
					}
					return true;
				case -2:
					if(this.selectedItemIndex < this.entryItems.size()-1)
					{
						if(!this.parent.getItem().getDetailStatus())
							this.calcPlusIndex(DETAIL_OPTION);
						else
							this.calcPlusIndex(NORMAL_OPTION);
					}
					return true;
				case -5:
					//fire button
					switch(this.selectedItemIndex)
					{
						case 0:
							//delete
							if(this.parent.getPersonCount() != 0)
								this.parent.deletePerson();
							break;
						case 1:
							//edit option
							if(this.parent.getSelectedIndex() != 0)
								this.optionStatePressed = STATE_OPEN_EDIT_ANIMATION;
							break;
						case 2:
							//add option
							this.optionStatePressed = STATE_OPEN_ADD_OPTION_ANIMATION;
							break;
						case 3:
							//add person
							this.optionStatePressed = STATE_OPEN_ADD_PERSON_ANIMATION;
							break;
						case 4:
							//Call a Person
							this.optionStatePressed = STATE_CALL_PERSON_ANIMATION;
							break;
						case 5:
							//Send A SMS
							this.optionStatePressed = STATE_SEND_SMS_ANIMATION;
							break;
						case 6:
							//sync with .csv
							this.optionStatePressed = STATE_SYNC_WITH_CSV_ANIMATION;
							break;
						case 7:
							//import a picture
							this.optionStatePressed = STATE_IMPORT_PICTURE_ANIMATION;
							break;
					}
					this.animationType = ANIMATION_CLOSE;
					this.animator.closeItem();
					return true;
				case -7:
					if(this.state == STATE_OPENED)
					{
						//close item
						this.animationType = ANIMATION_CLOSE;
						this.animator.closeItem();
						this.state = STATE_CLOSE_ANIMATION;
					}
					else if(this.state == STATE_CLOSED)
					{
						//open item
						this.animationType = ANIMATION_CLOSE;
						this.animator.closeItem();
						this.state = STATE_OPEN_ANIMATION;
					}
					return true;
			}
		}
		return false;
	}
	
	public void drawEntries(Graphics _g, Point _pos)
	{
		ListItem currentItem = null;
		int x = (int)_pos.getX();
		int y = (int)_pos.getY();
		int i = 0;
		Enumeration e = this.entryItems.elements();
        while( e.hasMoreElements() )
        {
        	if(!this.parent.getItem().getDetailStatus())
            {
        	 	_g.translate( x - _g.getTranslateX(), y - _g.getTranslateY() );
	            currentItem = (ListItem)e.nextElement();
	            if(currentItem.getShowOption() != DETAIL_OPTION)
	            {
	            	currentItem.paint( _g, i == this.selectedItemIndex);
	            	y += currentItem.getHeight();
	            }
	            i++;
            }
        	else
        	{
        	 	_g.translate( x - _g.getTranslateX(), y - _g.getTranslateY() );
	            currentItem = (ListItem)e.nextElement();
	            if(currentItem.getShowOption() != NORMAL_OPTION)
	            {
		            currentItem.paint( _g, i == this.selectedItemIndex);
		            y += currentItem.getHeight();
	            }
	            i++;
        	}
        }
	}
	

	public void animationEvent(int event) 
	{
		super.animationEvent(event);
		if( event == IAnimationEventListener.ANIMATION_STOPPED )
        {
    		switch(this.optionStatePressed)
    		{
    			case STATE_OPEN_EDIT_ANIMATION:
    				this.parent.openEditItem();
                	this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_OPEN_ADD_PERSON_ANIMATION:
    				this.parent.openAddPersonItem();
                	this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_OPEN_ADD_OPTION_ANIMATION:
    				this.parent.openAddPersonOptionItem();
                	this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_SEND_SMS_ANIMATION:
    				this.parent.openSMS();
                	this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_CALL_PERSON_ANIMATION:
    				this.parent.openCallPerson();
                	this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_IMPORT_PICTURE_ANIMATION:
    				this.parent.openPictureManger();
    				this.optionStatePressed = STATE_NOTHING;
    				break;
    			case STATE_SYNC_WITH_CSV_ANIMATION:
    				this.parent.openCSVBrowser();
    				this.optionStatePressed = STATE_NOTHING;   	
    				break;
    		}
        }
	}
}
